<!DOCTYPE html>
<title>Test that multiple audio elements can play concurrently.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
async_test(function(t) {
    // Number of concurrent audio elements to test.
    // For larger values a longer media file is needed.
    var maxPlayers = 2;
    var audioElementCount = 0;

    for (var i = 0; i < maxPlayers; i++) {
        var audio = document.createElement("audio");
        document.documentElement.appendChild(audio);
    }

    testAudioElement(0);

    function testAudioElement(count) {
        var audioElement = document.querySelectorAll("audio")[count];
        audioElement.onerror = t.unreached_func();
        audioElement.onplaying = t.step_func(function() {});

        audioElement.oncanplaythrough = t.step_func(function(event) {
             var currentAudio = event.target;
             assert_equals(currentAudio.currentTime, 0, "audio element " + (audioElementCount + 1) + " at start of playback");
             currentAudio.play();
        });

        audioElement.ontimeupdate = t.step_func(function(event) {
            var currentAudio = event.target;
            assert_greater_than(currentAudio.currentTime, 0, "audio element " + (audioElementCount + 1) + " during playback");
            currentAudio.ontimeupdate = null;
            if (++audioElementCount == maxPlayers) {
                // All audio elements have been started playing.
                // Make sure all of them are still playing.
                for (var i = 0; i < maxPlayers; i++) {
                    currentAudio = document.getElementsByTagName('audio')[i];
                    assert_false(currentAudio.paused, "audio element " + (i + 1) + " during playback");
                }
                t.done();
            } else {
                // Start the next audio element.
                testAudioElement(audioElementCount);
            }
        });

        audioElement.src = "content/silence.oga";
    }
});
</script>